package com.ermao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ermao.mapper.AccountMapper;
import com.ermao.mapper.AdminUserMapper;
import com.ermao.mapper.CategoryMapper;
import com.ermao.mapper.UserMapper;
import com.ermao.pojo.AdminUser;
import com.ermao.pojo.User;
import com.ermao.pojo.result.admin.AdminGlobalResult;
import com.ermao.pojo.result.user.UserInGetInformation;
import com.ermao.pojo.result.user.UserInPunchRank;
import com.ermao.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ermao
 * Date: 2021/9/5 10:36
 */
@Service
public class AdminServiceImpl implements AdminService {


	private AdminUserMapper adminUserMapper;
	private UserMapper userMapper;
	private AccountMapper accountMapper;
	private CategoryMapper categoryMapper;

	@Autowired
	public AdminServiceImpl(AdminUserMapper adminUserMapper, UserMapper userMapper, AccountMapper accountMapper, CategoryMapper categoryMapper) {
		this.adminUserMapper = adminUserMapper;
		this.userMapper = userMapper;
		this.accountMapper = accountMapper;
		this.categoryMapper = categoryMapper;
	}

	@Override
	public boolean login(Integer accountNumber, String password) {
		QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("admin_account_number", accountNumber);
		queryWrapper.eq("password", password);
		AdminUser adminUser = adminUserMapper.selectOne(queryWrapper);
		return adminUser != null;
	}

	@Override
	public AdminGlobalResult getGlobalInformation() {
		Integer totalUsersAmount = userMapper.selectCount(null).intValue();
		Long totalAccountsAmount = accountMapper.selectCount(null);
		Integer totalCategoriesAmount = categoryMapper.selectCount(null).intValue();
		return new AdminGlobalResult(totalUsersAmount, totalAccountsAmount, totalCategoriesAmount);
	}

	@Override
	public List<UserInPunchRank> getUsersInPunchRank(Integer number) {
		List<UserInPunchRank> rel = new ArrayList<>();
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("user_id", "nickname" , "days", "create_time");
		queryWrapper.orderBy(true, false, "days");
		queryWrapper.last("limit " + number);
		List<User> users = userMapper.selectList(queryWrapper);
		users.forEach(user -> {
			rel.add(new UserInPunchRank(user.getUserId(), user.getNickname(), user.getDays(), user.getCreateTime()));
		});
		return rel;
	}

	@Override
	public UserInGetInformation getUserInformationByAccountNumber(Integer accountNumber) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("user_id", "account_number", "nickname", "days", "email");
		queryWrapper.eq("account_number", accountNumber);
		User user = userMapper.selectOne(queryWrapper);
		if (user == null) {
			return null;
		} else {
			return new UserInGetInformation(user.getUserId(),user.getAccountNumber(), user.getNickname(), user.getDays(), user.getEmail());
		}
	}

	@Override
	public boolean updateUserPassword(Integer accountNumber, String password) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("account_number", accountNumber);
		User temp = userMapper.selectOne(queryWrapper);
		if (temp == null) {
			return false;
		}
		User user = new User();
		user.setUserId(temp.getUserId());
		user.setPassword(password);
	 	return userMapper.updateById(user) > 0;
	}

	@Override
	public boolean updateUserNickname(Integer userId, String nickname) {
		User user = new User();
		user.setUserId(userId);
		user.setNickname(nickname);
		return userMapper.updateById(user) > 0;
	}
}
